package io.adrainty.algorithm.leetcode.normal;

/**
 * @author adrainty
 * @version v1.0.0
 * @since 2024/12/23 上午11:17
 */
@SuppressWarnings("unused")
public class Solution61 {

     public static class ListNode {
         int val;
         ListNode next;
         ListNode() {}
         ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     }

    public static ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null) return head;
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        k %= size;
        if (k == 0) return head;
        cur = head;
        for (int i = 0; i < size - k - 1; i++) {
            cur = cur.next;
        }
        ListNode newHead = cur.next;
        cur.next = null;
        cur = newHead;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head;
        return newHead;
    }
    
}
